package Web;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

public class Response {
	private StringBuilder headInfo;
	private StringBuilder content;
	private BufferedWriter bw;
	private static final String CRLF = "\r\n";
	private static final String BLANK = " ";
	private int len=0;
	public Response(){
		headInfo = new StringBuilder();
		content = new StringBuilder();
		len = 0;
	}
	public Response(OutputStream os){
		this();
		bw = new BufferedWriter(new OutputStreamWriter(os));
	}
    public Response print(String info){
    	content.append(info);
    	len+=info.getBytes().length;
    	return this;
    }
    public Response println(String info){
    	content.append(info).append(CRLF);
    	len+=(info+CRLF).getBytes().length;
    	return this;
    }
	private void createHeadInfo(int code){
		headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);
		switch(code){
			case 200:
			headInfo.append("OK");
			break;
			case 404:
			headInfo.append("NOT FOUND");
			break;
			case 505:
				headInfo.append("SERVER ERROR");
				break;
		}
		headInfo.append(CRLF);
		headInfo.append("Server:bjsxt Server/0.0.1").append(CRLF);
		headInfo.append("Date:").append(new Date()).append(CRLF);
		headInfo.append("Content-type:text/html;charset=GBK").append(CRLF);
		headInfo.append("Content Length:").append(len).append(CRLF);
		
		headInfo.append(CRLF);
	
	}
	void pushToClient(int code) throws IOException{
		createHeadInfo(code);
		bw.append(headInfo.toString());
		bw.append(content.toString());
		bw.flush();
	}
	
}
